package com.china08.yxyapi.api.manage.tec.teaching.homework.worklist.workinfo.report.lookwork;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.entity.es.ElasticYxyYxyQuesResource;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskQues;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskQues.Article;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReport;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReportQues;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskQuesRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportQuesRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportRepository;
import com.china08.yxyapi.service.manage.tec.teaching.homework.ElasticQuesService;
import com.china08.yxyapi.util.DateUtils;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

/**
 * 作业系统 查看作业
 */
@CrossOrigin
@RestController
public class HomeworkReportLookV1Controller {

    @Autowired
    private HomeworkTaskReportQuesRepository homeworkTaskReportQuesRepository;

    @Autowired
    private HomeworkTaskReportRepository homeworkTaskReportRepository;

    @Autowired
    ElasticQuesService elasticQuesService;

    @Autowired
    private HomeworkTaskQuesRepository homeworkTaskQuesRepository;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/tec/teaching/homework/worklist/workinfo/report/lookwork.v1", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess( //
            @RequestParam String taskId, //
            @RequestParam String studentId, //
            RespModel respModel) throws JsonParseException, JsonMappingException, IOException {
        // 查询作业标题 班级名 作业用时 正确率 评语
        HomeworkTaskReport homeworkTaskReport = homeworkTaskReportRepository.findByStudentIdAndTaskId(studentId,
                taskId);
        // 获取当前时间
        Date currentDate = new Date();
        // 查询当前学生在当前任务下的截止时间
        Date predictDate = homeworkTaskReport.getPredictDate();
        if (predictDate.before(currentDate)) {
            respModel.booTime = 1;
        } else {
            respModel.booTime = 0;
        }
        respModel.title = homeworkTaskReport.getTitle();
        respModel.studentNick = homeworkTaskReport.getStudentNick();
        respModel.className = homeworkTaskReport.getClassName();
        respModel.duration = DateUtils.getDurationDateByDurationCount(homeworkTaskReport.getDuration() * 1000);
        respModel.statusOfFinished = homeworkTaskReport.getStatusOfFinished();
        // 将double类型的数据保存到小数点的后两位
        DecimalFormat df = new DecimalFormat("######0.0");
        respModel.rightRate = Double.parseDouble(df.format(homeworkTaskReport.getRightRate()));
        respModel.evaluate = homeworkTaskReport.getEvaluate();

        // 获取本次任务学生的回答
        List<HomeworkTaskReportQues> homeworkTaskReportQuesLst = homeworkTaskReportQuesRepository
                .findByStudentIdAndTaskId(studentId, taskId);

        // ##
        // 单选题、多选题、填空题、简单题
        List<HomeworkTaskReportQues0134Model> homeworkTaskReportQues0134Lst = new ArrayList<HomeworkTaskReportQues0134Model>();
        // ##
        // 通知类试题
        HomeworkTaskReportQues2Model homeworkTaskReportQues2 = new HomeworkTaskReportQues2Model();
        // ##
        // 阅读类试题
        HomeworkTaskReportQues5Model homeworkTaskReportQues5 = new HomeworkTaskReportQues5Model();

        // ##
        // 获取本次任务试题 (单选题、多选题、填空题、简单题)
        List<HomeworkTaskQues> homeworkTaskQues01234Lst = homeworkTaskQuesRepository
                .findByTaskIdAndQuesTypeInOrderByOrderAsc(taskId, new String[] { "0", "1", "3", "4" });
        List<String> quesIds = new ArrayList<>();
        for (HomeworkTaskQues homeworkTaskQues : homeworkTaskQues01234Lst) {
            quesIds.add(homeworkTaskQues.getQuesId());
        }
        List<ElasticYxyYxyQuesResource> esLst = null;
        if (quesIds.size() > 0) {
            esLst = elasticQuesService.findByQuesIdIn(quesIds);
        }
        for (HomeworkTaskQues homeworkTaskQues : homeworkTaskQues01234Lst) {
            HomeworkTaskReportQues0134Model model = new HomeworkTaskReportQues0134Model();
            model.quesId = homeworkTaskQues.getQuesId();
            model.order = homeworkTaskQues.getOrder();
            model.quesBody = null;
            model.questionType = homeworkTaskQues.getQuesType();
            model.passed = null;
            model.answerOfMy = null;
            // es 试题
            esLst.forEach(es -> {
                if (es.getId().equals(model.quesId)) {
                    model.quesBody = es;
                }
            });
            // 学生的回答
            homeworkTaskReportQuesLst.forEach(homeworkTaskReportQues -> {
                if (homeworkTaskReportQues.getQuesId().equals(model.quesId)) {
                    model.passed = homeworkTaskReportQues.getPassed();
                    model.answerOfMy = homeworkTaskReportQues.getAnswerOfMy();
                    model.mark = homeworkTaskReportQues.getMark();
                    model.answerText = homeworkTaskReportQues.getAnswerText();
                    model.answerImgs = homeworkTaskReportQues.getAnswerImgs();
                }
            });
            homeworkTaskReportQues0134Lst.add(model);
        }

        // ##
        // 获取本次任务通知类试题
        HomeworkTaskQues HomeworkTaskQues2 = homeworkTaskQuesRepository.findTop1ByTaskIdAndQuesType(taskId, "2");
        if (HomeworkTaskQues2 != null) {
            homeworkTaskReportQues2.quesId = HomeworkTaskQues2.getQuesId();
            homeworkTaskReportQues2.quesContent = HomeworkTaskQues2.getQuesContent();
            homeworkTaskReportQues2.quesArticle = HomeworkTaskQues2.getQuesArticle();
            homeworkTaskReportQues2.questionType = HomeworkTaskQues2.getQuesType();
            homeworkTaskReportQues2.mark = null;
            homeworkTaskReportQues2.answerText = null;
            homeworkTaskReportQues2.answerImgs = null;
            // 学生的回答
            homeworkTaskReportQuesLst.forEach(homeworkTaskReportQues -> {
                if (homeworkTaskReportQues.getQuesId().equals(HomeworkTaskQues2.getQuesId())) {
                    homeworkTaskReportQues2.mark = homeworkTaskReportQues.getMark();
                    homeworkTaskReportQues2.answerText = homeworkTaskReportQues.getAnswerText();
                    homeworkTaskReportQues2.answerImgs = homeworkTaskReportQues.getAnswerImgs();
                }
            });
        }

        // ##
        // 获取本次任务阅读类试题
        HomeworkTaskQues HomeworkTaskQues5 = homeworkTaskQuesRepository.findTop1ByTaskIdAndQuesType(taskId, "5");
        if (HomeworkTaskQues5 != null) {
            homeworkTaskReportQues5.quesId = HomeworkTaskQues5.getQuesId();
            homeworkTaskReportQues5.quesArticle = HomeworkTaskQues5.getQuesArticle();
            homeworkTaskReportQues5.questionType = HomeworkTaskQues5.getQuesType();
            homeworkTaskReportQues5.mark = null;
            // 学生的回答
            homeworkTaskReportQuesLst.forEach(homeworkTaskReportQues -> {
                if (homeworkTaskReportQues.getQuesId().equals(HomeworkTaskQues5.getQuesId())) {
                    homeworkTaskReportQues5.mark = homeworkTaskReportQues.getMark();
                    homeworkTaskReportQues5.quesArticle = homeworkTaskReportQues.getQuesArticle();
                }
            });
        }

        // return
        respModel.homeworkTaskReportQues0134Lst = homeworkTaskReportQues0134Lst;
        respModel.homeworkTaskReportQues2 = homeworkTaskReportQues2;
        respModel.homeworkTaskReportQues5 = homeworkTaskReportQues5;
        return respModel;
    }

    protected static class RespModel extends YxyApiRespModel {
        // 查看作业页面的标题
        public String title;
        // 所在班级的名称
        public String className;
        // 学生名称
        public String studentNick;
        // 作业用时
        public String duration;
        // 正确率
        public Double rightRate;
        // 教师评语
        public String evaluate;
        // 判断当前时间是否超过截止时间
        public Integer booTime;
        // 提交状态
        public Integer statusOfFinished;
        // 单选题、多选题、填空题、简单题
        public List<HomeworkTaskReportQues0134Model> homeworkTaskReportQues0134Lst;
        // 通知类试题
        public HomeworkTaskReportQues2Model homeworkTaskReportQues2;
        // 阅读类试题
        public HomeworkTaskReportQues5Model homeworkTaskReportQues5;
    }

    // 单选题、多选题、填空题、简单题
    public class HomeworkTaskReportQues0134Model {
        // 题id
        public String quesId;
        // 题的序号
        public Integer order;
        // 题的详细信息
        public Object quesBody;
        // 题目的类型
        public String questionType;
        // 试题类作业是否正确 0-错误 , 1-正确
        public Integer passed;
        // 学生的回答 - (客观题) 选择题
        public String answerOfMy;
        // 学生的回答 - (主观题) 文本
        public String answerText = null;
        // 学生的回答 - (主观题) 图片
        public List<String> answerImgs = new ArrayList<String>();
        // 是否展示解析
        public boolean isShow = false;
        // 是否展示学生答案
        public boolean isStuShow = false;
        // 评分
        public Integer mark;
    }

    // 通知类试题
    public class HomeworkTaskReportQues2Model {
        public String quesId = null;
        public String quesContent = null; // 原题-文本
        public List<Article> quesArticle = new ArrayList<Article>(); // 原题-文档
        public String questionType = null;
        public Integer mark = null;
        public String answerText = null; // 学生的回答-文本
        public List<String> answerImgs = new ArrayList<String>(); // 学生的回答-图片
        public boolean isStuShow = false; // 是否展示学生答案
    }

    // 阅读类试题
    public class HomeworkTaskReportQues5Model {
        public String quesId = null;
        public List<Article> quesArticle = new ArrayList<Article>(); // 原题-文档
        public String questionType = null;
        public Integer mark = null;
    }
}
